package com.hrm.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hrm.common.R2;
import com.hrm.javabean.UserInf;
import com.hrm.service.UserInfService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.apache.commons.lang.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserInfController {
    @Autowired
    UserInfService userInfService;

    // 登录
    @PostMapping("/login")
    public void login(HttpServletRequest request, HttpServletResponse response, String loginname, String password, String code) throws IOException, ServletException {
        // 判断验证码
        if (!request.getSession().getAttribute("code1").equals(code)) {
            request.getSession().setAttribute("tips", "验证码错误");
            response.sendRedirect("/res/user/login.jsp");
            return;
        }
        List<Object> userInf = userInfService.login(loginname, password);

        if (userInf == null) {
            request.getSession().setAttribute("tips", "账号不存在/密码错误");
            response.sendRedirect("/res/user/login.jsp");
            return;
        }
        // 状态为3，审核不通过
        if (userInf.get(0).equals("3")) {
            request.getSession().setAttribute("tips", "账号审核不通过，请重新注册");
            response.sendRedirect("/res/user/login.jsp");
            return;
        }
        // 状态为0，正在审核
        if (userInf.get(0).equals("0")) {
            request.getSession().setAttribute("tips", "账号正在审核，请稍后");
            response.sendRedirect("/res/user/login.jsp");
            return;
        }
        // 登录成功 将user放入session
        request.getSession().setAttribute("user", userInf.get(1));
        response.sendRedirect("/res/index.jsp");
    }

    // 查询登录名是否已被注册
    @GetMapping("/checkLoginName")
    public void checkLoginName(HttpServletResponse response, String loginname) throws IOException {
        System.out.println("loginname:" + loginname);
        int i = userInfService.checkLoginName(loginname);
        response.getWriter().write(i + "");
    }

    // 注册
    @PostMapping("/register")
    public void register(HttpServletRequest request, HttpServletResponse response, UserInf userInf, String code) throws IOException, ServletException {
        if (!request.getSession().getAttribute("code2").equals(code)) {
            // 验证码有误
            response.getWriter().write("-1");
            return;
        }
        int i = userInfService.addUser(userInf);
        response.getWriter().write(i + "");
    }

    @GetMapping("/aaa")
    public void aaa(HttpServletRequest request, HttpServletResponse response) throws IOException {
        System.out.println("aaa");
        response.getWriter().write("aasasassa");
    }

    // 获取待审核人数
    @GetMapping("/getNewUserNum")
    public void getNewUserNum(HttpServletResponse response) throws IOException {
        LambdaQueryWrapper<UserInf> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserInf::getStatus, "0");
        int count = userInfService.count(wrapper);
        response.getWriter().write(count + "");
    }

    // 分页查询
    @GetMapping("/page")
    public void page(HttpServletResponse response, int page, int limit, String userName, String status) throws IOException {
        log.info("page = {},limit = {},userName = {},status ={}", page, limit, userName, status);
        // 分页构造器
        Page pageInfo = new Page(page, limit);
        // 条件构造
        LambdaQueryWrapper<UserInf> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.like(StringUtils.isNotEmpty(userName), UserInf::getUserName, userName);
        queryWrapper.eq(StringUtils.isNotEmpty(status), UserInf::getStatus, status);
        // 已删除的用户不显示
        queryWrapper.in(UserInf::getStatus, 0, 1, 3);
        // 排序
        queryWrapper.orderByDesc(UserInf::getCreateDate);
        // 执行查询
        userInfService.page(pageInfo, queryWrapper);

        R2<List<UserInf>> r2 = new R2();
        r2.setCode(0);
        r2.setCount(pageInfo.getTotal());
        r2.setData(pageInfo.getRecords());
        System.out.println(JSON.toJSONString(r2));
        response.getWriter().write(JSON.toJSONString(r2));
    }

    // 更新用户
    @PostMapping("/update")
    public void update(HttpServletResponse response, UserInf userInf) throws IOException {
        boolean update = userInfService.updateById(userInf);
        // 更新成功返回1
        if (update) {
            response.getWriter().write("0");
        } else {
            response.getWriter().write("-1");
        }
    }


}
